// pages/receipt/receipt.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    stars: [0, 1, 2, 3, 4],
    orderId: 0,
    picUrl: [],
    receiveUserId: 0,
    normalSrc: '../../icons/icon_xx.png',
    selectedSrc: '../../icons/icon_xx_selected.png',
    icon: [{
        picUrl: "../../icons/icon_good.png"
      },
      {
        picUrl: "../../icons/icon_normal.png"
      },
      {
        picUrl: "../../icons/icon_bad.png"
      }
    ],
    iconSelect: [{
        picUrl: "../../icons/icon_good_pre.png"
      },
      {
        picUrl: "../../icons/icon_normal_pre.png"
      },
      {
        picUrl: "../../icons/icon_bad_pre.png"
      }
    ],
    des: "",
    key: 5,
    // imageSrc: ['../../icons/jia.png'],
  },

  selectLeft: function(e) {
    var key = e.currentTarget.dataset.key
    if (this.data.key == 0.5 && e.currentTarget.dataset.key == 0.5) {
      //只有一颗星的时候,再次点击,变为0颗
      key = 0;
    }
    console.log("得" + key + "分")
    this.setData({
      key: key
    })

  },
  //点击左边,整颗星
  selectRight: function(e) {
    var key = e.currentTarget.dataset.key
    console.log("得" + key + "分")
    this.setData({
      key: key
    })
  },
  // 评分
  good: function() {
    this.setData({
      key: 5
    })
  },
  normal: function() {
    this.setData({
      key: 3
    })
  },
  bad: function() {
    this.setData({
      key: 1
    })
  },
 
  // 评价内容
  des: function(e) {
    this.setData({
      des: e.detail.value
    })
  },
  // 提交评分
  grabOrder: function() {
    var that = this;
  
    wx.showModal({
      title: '提示',
      content: '是否确认提交评价',
      confirmText: "确认",
      cancelText: "取消",
      success: function(res) {
        if (res.confirm) {
          console.log(that.data.orderId)
          that.setData({
            orderId:parseInt(that.data.orderId)
          })
          console.log(that.data.des)
          console.log(that.data.key)
          wx.request({
            url: 'https://www.xiaozheng8.com/service/comment/add',
            // url: 'https://www.xiaozheng8.com/serviceOrder/setStatus',
            method: "post",
            data: {
              orderId: that.data.orderId,
              des: that.data.des,
              grade: that.data.key,
              // status: 11,
              // img: that.data.imageSrc,
              // userId: wx.getStorageSync("userId")
              userId: wx.getStorageSync("userId")
            },
            success: function(res) {
              console.log(res);
              if (res.data.code == 200) {
                // wx.navigateBack({})
                wx.showToast({
                  title: '提交成功',
                  icon: 'success',
                  duration: 1000,
                  mask: true
                })
                // wx.navigateBack({})
                wx.reLaunch({
                  url: '../order/order',
                })
                // wx.redirectTo({
                //   url: '../servering/servering?userId=' + that.data.orderId +"&receiveUserId="+that.data.receiveUserId,
                // })


              } else {
                wx.showModal({
                  title: '提示',
                  content: '信息提交失败，请重新上传',
                  showCancel: false,
                  success: function(res) {}
                })
              }

            },
            fail: function(res) {
              console.log(res);
            }
          })

        };

      }
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that=this;
    console.log(options);
    this.setData({
      orderId: options.orderId,
      receiveUserId: options.receiveUserId


    })
    console.log(that.data.receiveUserId)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  post: function(e) {
    var that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album'],
      success: function(res) {
        that.setData({
          picUrl: res.tempFilePaths
        })
        let num = res.tempFilePaths.length;
        let imgs = res.tempFilePaths;
        for (let i = 0; i < num; i++) {
          wx.uploadFile({
            url: 'https://www.xiaozheng8.com/upload',
            filePath: imgs[i],
            name: 'file',
            header: {
              'Content-Type': 'multipart/form-data'
            },
            success: function(res) {
              console.log(res.data);
              that.data.picUrl[i] = res.data;
              that.setData({
                picUrl: that.data.picUrl
              })
              console.log(that.data.picUrl)
            }
          })
        }

      },
    })
  },
  // cancel: function() {
  //   this.setData({
  //     picUrl: []
  //   })
  // },



  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})